// pages/component/homepage/homepage.js
var Api = require("../../common/api/api.js")
var App = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentNum: 0,
    content: "",
    createdTime: "",
    headImg: "",
    id: 0,
    nickname: "",
    shareNum: 0,
    staticRes: [],
    thumbUp: false,
    thumbUpNum: 0,
    topicComments: [],

    //点赞按钮
    thumbUpButtonFalse: "../../../image/icon/dianzan.png",
    thumbUpButtonTrue: "../../../image/icon/dianzan_default.png",

    //评论内容
    commentContent: "",

    //文本框焦点
    inputShowed: false,

    //input框提示文字
    placeholder: '你有什么看法?',

    //类型；1：评论话题、2：回复评论
    type:1,


    //评论ID type=2时有效
    commentId:0,
    //回复的用户ID type=2时有效
    userId:0,

    inputStatus:false,
  },
  

  // 点击分享
  onShareAppMessage: function (e) {
    let _this = this;

    var id = e.target.dataset.id;

    var data = {
      id: id
    }
    var params = JSON.stringify(data)
    Api.addShare(params).then(function (result) {
      _this.getDetail(id);
    }).catch(function (error) {
      console.log(error)
    })

    return {
      title: _this.data.content.substring(0, 10) +"...",
      desc: '分享页面的内容',
      path: '/pages/component/homepage/homepage?id='+id, // 路径，传递参数到指定页面。
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //话题主键ID
    this.getDetail(options.id);
  },


  getDetail:function(id){
    let _this = this;

    //请求参数封装
    var data = {
      id: id,
      openid: wx.getStorageSync("openid"),
    }
    var params = JSON.stringify(data)

    //获取话题详情
    Api.getTopicDetail(params).then(function (result) {
      _this.setData(
        result.data.data
      );
    }).catch(function (error) {
      console.log(error)
    })
  },


  /**
   * 点赞
   */
  thumbUp: function () {
    let _this = this;
  
    //话题主键ID
    var id = _this.data.id;

    //1：点赞；2：取消点赞
    var type = 1;
    if (_this.data.thumbUp){
      type = 2;
    }

    //请求参数封装
    var data = {
      id: id,
      openid: wx.getStorageSync('openid'),
      type: type,
    }
    var params = JSON.stringify(data)

    Api.thumbsUpOperation(params).then(function (result) {
      _this.getDetail(id);
      if(type==1){
        App.showSuccess("点赞成功");
      }else{
        App.showSuccess("取消成功");
      }
    }).catch(function (error) {
      console.log(error)
    })
  },

  /**
   *  小程序数据双向绑定 
   */
  inputeidt: function (e) {
    this.setData({
      commentContent: e.detail.value,
    });
  },

  /**
   * 评论
   */
  comment:function(){
    let _this = this;
    
    //话题主键ID
    var id = _this.data.id;
    
    if(_this.data.commentContent!=""){
      //请求参数封装
      var data = {
        id: id,
        openid: wx.getStorageSync('openid'),
        content: _this.data.commentContent,
        type: _this.data.type,
        userId: _this.data.userId,
        commentId: _this.data.commentId,
      }
      var params = JSON.stringify(data)

      Api.commentTopic(params).then(function (result) {
        if (result.data.code == 0){
          _this.getDetail(id);
          App.showSuccess("评论成功");
          _this.setData({
            commentContent: "",
            placeholder: '你有什么看法?',
          });   
        }
      }).catch(function (error) {
        console.log(error)
      })
    }
  },


  /**
   *  预览
   */
  previewImage: function (e) {
    let _this = this;

    var current = e.target.dataset.src;
    var id = e.target.dataset.id;

    var previewImgs = new Array();

    var items = _this.data.staticRes;

    for (var i = 0; i < items.length; i++) {
      previewImgs = previewImgs.concat(items[i].path);
    }

    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: previewImgs // 需要预览的图片http链接列表  
    })
  },


  //获取文本框焦点
  showInputFocus:function(){
    this.setData({
      inputShowed:true,
      placeholder: '你有什么看法?',
      type: 1,
      inputStatus:true,
    });
  },


  //获取文本框焦点并设置input框提示文字
  showInputFocusAndSetPlace: function (e) {
    var userId = e.currentTarget.dataset.user;
    var commentId = e.currentTarget.dataset.id;
    var nickname = e.currentTarget.dataset.nickname;
    var currentUserId = wx.getStorageSync("userId");

    console.log("回复操作用户ID: ", currentUserId);
    console.log("回复用户ID： ", userId);
    console.log("回复commentId：", commentId);
    console.log("回复用户昵称： ", nickname);

    if (userId != currentUserId){
     
      this.setData({
        inputShowed: true,
        placeholder: '回复：' + nickname,
        commentId: commentId,
        userId: userId,
        type: 2,
        inputStatus: true,
      });
    }
  },


  phoneOnBlur:function(){
    this.setData({
      inputStatus:false,
    });
  },


})